﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SQLite;

namespace 第十一次作业
{
    public partial class Form1 : Form
    {
        int i = 0;
        string[] chinese = new string[1000];                                    //字符串数组用以存储中文
        SQLiteConnection words;                                                    //数据库连接实例
        public Form1()
        {
            InitializeComponent();
            connectToDatabase();
            getChinese();
            CHN.Text = chinese[i];
            i++;
        }
        /*连接到数据库，在bin文件夹下，数据库数据事先用命令行录入*/
        public void connectToDatabase()
        {
            try
            {
                words = new SQLiteConnection("Data Source=test.db;Version=3;");
            }
            catch { }
        }
        /*获取数据库中文信息*/
       public void getChinese()
        {
            try
            {
                words.Open();
                int j = 0;
                string sqle = "select CHINESE from WORDS";
                SQLiteCommand command = new SQLiteCommand(sqle, words);
                SQLiteDataReader reader = command.ExecuteReader();
                while (reader.Read())
                {
                    chinese[j] = reader.GetString(0);
                    j++;
                }
                words.Close();
            }
            catch { }
        }
       /*点击文本框清除原先存在的提示输入信息*/
        private void englishBox_Click(object sender, EventArgs e)
        {
            try
            {
                englishBox.Clear();
            }
            catch { }
        }
        /*点击确定按钮之后开始核对*/
        private void confirm_Click(object sender, EventArgs e)
        {
            try
            {
                words.Open();
                string sql = "select ENGLISH from WORDS where CHINESE='" + CHN.Text + "'";
                SQLiteCommand command = new SQLiteCommand(sql, words);
                SQLiteDataReader reader = command.ExecuteReader();
                while (reader.Read())
                {
                    //Console.Write(reader.GetString(0));
                    if (englishBox.Text == reader.GetString(0))
                    {//如果答对了，直接下一个
                        englishBox.Clear();
                        CHN.Text = chinese[i];
                        i++;
                    }
                    else
                    {//如果答错
                        englishLabel.Text = reader.GetString(0);
                    }
                }
                words.Close();
            }
            catch { }
        }
        /*点击>按钮触发事件，下一个单词*/
        private void next_Click(object sender, EventArgs e)
        {
            try
            {
                englishBox.Clear();
                englishLabel.Text = "";
                CHN.Text = chinese[i];
                i++;
            }
            catch {
                MessageBox.Show("单词数量越界！请勿前进！");
            }
        }
        /*点击<按钮触发事件，上一个单词*/
        private void back_Click(object sender, EventArgs e)
        {
            try
            {
                englishBox.Clear();
                englishLabel.Text = "";
                i = i - 2;
                CHN.Text = chinese[i];
                i++;
            }
            catch {
                MessageBox.Show("单词数量越界！请勿回退！");
            }
        }
    }
}